<?php

namespace app\admin\service;

use app\common\logic\CommonLogic;
use app\common\model\trRentalOutlets;
use app\common\model\trResource;
use app\common\util\ErrorCode;
use app\common\util\Tools;

/**
 * Class OutletsService
 * @package app\admin\service
 * 网点服务类
 */
class OutletsService extends BaseService
{
    protected $field = [
        "name" => self::FIELD_REQUIRE, "mobile" => self::FIELD_REQUIRE, "address" => self::FIELD_REQUIRE,
        "province_code" => self::FIELD_REQUIRE, "city_code" => self::FIELD_REQUIRE,
        "area_code" => 0, "score" => 0, 'pic_id' => '',
        'business_hours' => self::FIELD_REQUIRE, "service_fee" => 0, 'deliver_range' => 0
    ]; //填入数据库需要存储的表字段

    // 获取网点列表
    public function getList(){
        $page = isset($this->params['page']) ? $this->params['page'] : 1;
        $pageSize = isset($this->params['pageSize']) ? $this->params['pageSize'] : 10;

        $db = trRentalOutlets::where(["mall_id" => $this->mall_id])
            ->field("id,number_id,name,mobile,province_code,city_code,area_code,address,score,business_hours,status");
        if(!empty($this->params["searchValue"])){
            $db->where([["name|number_id", "like", $this->params["searchValue"]]]);
        }
        $data = $db->paginate(["list_rows" => $pageSize, "page" => $page]);
        $this->returnData["data"] = $this->convertPageData($data);
        foreach ($this->returnData["data"]["list"] as &$item){
            $address = Tools::getNameByPCA($item["provinceCode"], $item["cityCode"], $item["areaCode"]);
            $item["address"] = str_replace(" ", "", $address).$item["address"];
        }
        return $this->returnData;
    }

    public function getInfo(){
        if(empty($this->params["id"])){
            return ErrorCode::code("params_not_exist", "id");
        }
        $database = trRentalOutlets::where("id", $this->params["id"])->find();
        if(empty($database)){
            return  ErrorCode::code("customized", "数据不存在");
        }
        $data = $this->convertData($database->toArray());
        $data['picId'] = explode(",", $data["picId"]);
        foreach ($data["picId"] as $pic){
            $data["picUrl"][] = [
                "url" => tome_dia(trResource::getValue("url", ["id" => $pic])),
                "id" => $pic
            ];
        }
        $data["provinceCode"] = strval($data["provinceCode"]);
        $data["cityCode"] = strval($data["cityCode"]);
        $data["areaCode"] = strval($data["areaCode"]);
        $this->returnData["data"] = $data;
        return $this->returnData;
    }

    public function toAdd(){
        $numberId = trRentalOutlets::getNumberId();
        $data = $this->extractData([
            "mall_id" => $this->mall_id, "number_id" => $numberId,
            "create_time" => time(), "update_time" => time()
        ]);
        $address = Tools::getNameByPCA($data["province_code"], $data["city_code"], $data["area_code"]);
        $address = str_replace(" ", "", $address).$data["address"];
        $res = CommonLogic::getLatLon($address);
        $data = array_merge($data, [
            "longitude" => $res["lng"], "latitude" => $res["lat"]
        ]);
        $model = trRentalOutlets::create($data);
        if(!$model->id){
            $this->returnData = ErrorCode::code("customized", "添加失败");
        }else{
            $this->returnData["msg"] = "添加成功";
        }
        return $this->returnData;
    }

    public function toEdit(){
        if(empty($this->params["id"])){
            return ErrorCode::code("params_not_exist", "id");
        }
        $database = trRentalOutlets::where("id", $this->params["id"])->find();
        if(empty($database)){
            return  ErrorCode::code("customized", "数据不存在");
        }
        $data = $this->extractData(["update_time" => time()]);
        $address = Tools::getNameByPCA($data["province_code"], $data["city_code"], $data["area_code"]);
        $address = str_replace(" ", "", $address).$data["address"];
        $res = CommonLogic::getLatLon($address);
        $data = array_merge($data, [
            "longitude" => $res["lng"], "latitude" => $res["lat"]
        ]);
        $database->data($data);
        $database->save();
        $this->returnData["msg"] = "更新成功";
        return $this->returnData;
    }

    public function toUpdateStatus(){
        if(empty($this->params["id"])){
            return ErrorCode::code("params_not_exist", "id");
        }
        /** @var trRentalOutlets $database */
        $database = trRentalOutlets::where("id", $this->params["id"])->find();
        if(empty($database)){
            return  ErrorCode::code("customized", "数据不存在");
        }
        $database->status = $database->status === 1 ? 2 : 1;
        $database->save();
        $this->returnData["msg"] = "更新成功";
        return $this->returnData;
    }

    public function toDel(){
        if(empty($this->params["id"])){
            return ErrorCode::code("params_not_exist", "id");
        }
        $database = trRentalOutlets::where("id", $this->params["id"])->find();
        if(empty($database)){
            return  ErrorCode::code("customized", "数据不存在");
        }
        $database->delete();
        $this->returnData["msg"] = "删除成功";
        return $this->returnData;
    }
}